package com.study.jsoup;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class JsoupUpFile {

    //@Test
    public void getMenu() throws IOException {
        String url = "http://localhost:9690/thy/menu/java8stream";
        String body = Jsoup.connect(url).ignoreContentType(true).execute().body();
        System.out.println(body);

    }

    /**
     * jsoup模拟上传数据|表单数据 文件|
     * com.study.web.SysToolController#uploadOssFile
     */
    @Test
    void upFile() throws IOException {
        String filePath = "D:/tmp/1.jpg";
        String filePath2 = "D:/tmp/2.jpg";
        FileInputStream inputStream = FileUtils.openInputStream(new File(filePath));
        FileInputStream inputStream2 = FileUtils.openInputStream(new File(filePath2));
        Connection.Response response =
                Jsoup.connect("http://localhost:9590/thy/tools/upfile")
                        .userAgent("Mozilla/5.0")
                        .timeout(10 * 1000)
                        .method(Connection.Method.POST)
                        // 字段参数| 文件名称| 文件流
                        .data("fs", FilenameUtils.getName(filePath), inputStream)
                        .data("fs2", FilenameUtils.getName(filePath2), inputStream2)
                        .data("destination", "/welcome")
                        .followRedirects(true)
                        .execute();
        String body = response.body();

        System.out.println(body);
    }


    public void get() {
        String url = "";
        Map<String, String> map = new HashMap<>();
        Jsoup.connect(url).timeout(10 * 1000).method(Connection.Method.GET)
                .data(map);


    }

    @Test
    public void getPageNotFond() {
        try {
            String url = "http://localhost:9590/api/params";
            Connection connection = Jsoup.connect(url).ignoreContentType(true)
                    .method(Connection.Method.GET);

              Map<String, String> params= new HashMap<>();
            params.put("type","sync同步");
            params.put("id","12");
            params.put("protocol","FLV");
            params.put("passCode","0708KATDE000N");

            //访问参数
            Connection.Response resp = connection.data(params).execute();
            System.out.println(resp.statusCode());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
